package test.YTool;


import java.lang.reflect.Field;

class testBean{
    private int i;
    private String s;
    public int getI() {
        return i;
    }
    public void setI(int i) {
        this.i = i;
    }
    public String getS() {
        return s;
    }
    public void setS(String s) {
        this.s = s;
    }
}

/**
 * 快速生成dto和实体类转换字段的方法
 * @author zxh
 *
 */
public class EntityConverterUtil {
	/**
	 * 生成单个实体类的get方法
	 */
    @SuppressWarnings("rawtypes")
	public static void generateGetMethod(Class singleClass,String singleName) { 
		String variName = singleName;
		Class class1 = singleClass;
		Field[] fields = class1.getDeclaredFields();
		for (Field field : fields) {
			String name = field.getName();
			if (!"serialVersionUID".equals(name)) {
				name = toFirstCharUpperCase(name);
				System.out.println(variName + ".get" + name + "();");
			}
		}
		System.out.println("---------get End----------------");
		System.out.println("");

	}
	
	/**
	 * 生成单个实体类的set方法
	 */
    @SuppressWarnings("rawtypes")
	public static void generateSetMethod(Class singleClass,String singleName) { 
		String variName = singleName;
		Class class1 = singleClass;
		Field[] fields = class1.getDeclaredFields();
		for (Field field : fields) {
			String name = field.getName();//获取字段名
			String type = field.getType().getSimpleName();//获取字段类型
			if (!"serialVersionUID".equals(name)) {
				String methodName = toFirstCharUpperCase(name);
				System.out.println(variName + ".set" + methodName + "("+type+" "+name+");");
			}
		}
		System.out.println("---------set End----------------");
		System.out.println("");
	}
	
	/**
	 * 生成字段名相同的两个类的set方法
	 */
    @SuppressWarnings("rawtypes")
	public static void generateSameSetterMethod(Class sourceClass, String sourceName, String targetName) { 
		Field[] sourceFields = sourceClass.getDeclaredFields();
		for (Field field : sourceFields) {
			String name = field.getName();//获取字段名
			if (!"serialVersionUID".equals(name)) {
				String methodName = toFirstCharUpperCase(name);
				System.out.println(targetName + ".set" + methodName + "("+sourceName+".get"+methodName+"());");
			}
		}
		System.out.println("---------set End----------------");
		System.out.println("");
	}

	/**
	 * 首字母大写
	 * @param str
	 * @return
	 */
	public static String toFirstCharUpperCase(String str) {
		if (str != null && str != "") {
			 str = str.substring(0,1).toUpperCase()+str.substring(1);
		}
		return str;
	}
	
	public static void main(String[] args) {
		generateGetMethod(testBean.class, "enterpriseInfo");
		
		generateSetMethod(testBean.class, "enterpriseInfo");
		
		generateSameSetterMethod(testBean.class, "dto1","dto2");
	}
}
